<?php
include("Database.php");
/**
 * Class Mysql @extends Database
 */
final class Mysql extends Database
{
    static private $instance;

	/**
	  * @constructor
	  * initializes all the members in the parent class
	  * @param dbHost name of the database
	  * @param dbName uri of the database
	  * @param dbUser log
	  * @param dbPass password
	  */

    protected function __construct($dbHost=null, $dbName=null, $dbUser=null, $dbPass=null) {
        parent::__construct($dbHost, $dbName, $dbUser, $dbPass);
    }
	
	/**
	  * @singleton
	  * get the singleton instance of the class
	  */
	
    static function getInstance($dbHost, $dbName, $dbUser, $dbPass) {
        if(!Mysql::$instance) {
            Mysql::$instance = new Mysql($dbHost, $dbName, $dbUser, $dbPass);
        }
        return Mysql::$instance;
    }
	
	/**
	  * @setter
	  * set a property of the class
	  */
	
    public function __set($name, $value) {
        if (isset($name) && isset($value)) {
            parent::__set($name, $value);
        }
    }
	
	/**
	  * @getter
	  * get a property from the class
	  */

    public function __get($name) {
        if (isset($name)) {
            return parent::__get($name);
        }
    }
	
	/**
	  * Connected
	  * @returns boolean : resource is connected or not
	  */

    public function Connected() {
        if (is_resource($this->connection)) {
            return true;
        } else {
            return false;
        }
    }
	
	/**
	  * AffectedRows
	  * @returns the number of lines affected during the last INSERT, UPDATE or DELETE request on the connection
	  */

    public function AffectedRows() {
        return mysql_affected_rows($this->connection);
    }
	
	/**
	  * Open
	  * launch connection to the database
	  */
	
    public function Open() {
        if (is_null($this->database))
            die("MySQL database not selected");
        if (is_null($this->hostname))
            die("MySQL hostname not set");

        $this->connection = @mysql_connect($this->hostname, $this->username, $this->password);

        if ($this->connection === false)
        die("Could not connect to database. Check your username and password then try again.\n");

        if (!mysql_select_db($this->database, $this->connection)) {
            die("Could not select database");
        }
    }
	
	/**
	  * Close
	  * close the current connection to the database
	  */

    public function Close() {
        mysql_close($this->connection);
        $this->connection = null;
    }
	
	/**
	  * Query
	  * handle a query to the database
	  * @param sql the current connection to the database
	  * @returns result result of the query
	  */

    public function Query($sql) {
        if ($this->connection === false) {
            die('No Database Connection Found.');
        }

        $result = @mysql_query($sql,$this->connection);
        if ($result === false) {
            die(mysql_error());
        }
        return $result;
    }
	
	/**
	  * FetchArray
	  * fetch a result row as an associative array, a numeric array, or both
	  * fetch a result row as an associative array
	  * @param result the result to fetch
	  * @returns data data of the query
	  */

    public function FetchArray($result) {
        if ($this->connection === false) {
            die('No Database Connection Found.');
        }
       
        $data = @mysql_fetch_array($result);
        if (!is_array($data)) {
            die(mysql_error());
        }
        return $data;
    }
	
	/**
	  * FetchAssoc
	  * fetch a result row as an associative array
	  * @param result the result to fetch
	  * @returns data data of the query
	  */
	 
    public function FetchAssoc($result) {
        if ($this->connection === false) {
            die('No Database Connection Found.');
        }
        $data = @mysql_fetch_assoc($result);
        if (!is_array($data)) {
            die(mysql_error());
        }
        return $data;
    }
}
?>
